<?php


error_reporting(E_ALL);

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

# 自动加载
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

# 引入类
use app\components\LogComponent;
use app\events\LogEvent;
use yii\base\Event;
use app\behaviors\LogBehavior;
use yii\behaviors\SluggableBehavior;
use yii\di\ServiceLocator;
use yii\di\Container;
use app\models\SlugModel;

# 设置路径
Yii::setAlias('@app', realpath('../'));

# 实例化组件
## 1
$logConfig = [
    'class' => LogComponent::class,
    'prefix' => function () {
        return '';
    },
    // 'as StdLog' => 'app\behaviors\LogBehavior',
    // 'on ' . LogComponent::EVENT_BEFORE_LOG => function ($event) {
    //     echo "events[{$event->name}] from " . get_class($event->sender), PHP_EOL;
    // },
    // 'as LogBehavior' => 'app\behaviors\LogBehavior',
];

unset($logConfig['class']);
// $log = Yii::createObject($logConfig);
$log = new LogComponent($logConfig);
## 2
// $locator = new ServiceLocator;
// $locator->set('log', $logConfig);
// $log = $locator->log;

// $log = $locator->get('log');
// 对象级别事件
// $log->on(LogComponent::EVENT_BEFORE_LOG, function ($event) use ( $logConfig){
//     echo "events[{$event->name}] from " . get_class($event->sender), PHP_EOL;
//     $event->handled = true;
// });
// $log->on('*', function ($event) {
//     echo "events[{$event->name}] from " . get_class($event->sender), PHP_EOL;
// });
// 类级事件
// LogEvent::on(LogComponent::class, LogComponent::EVENT_BEFORE_LOG, function ($event) {
//     // var_dump($event->sender);die;
//     echo "events[{$event->name}] from " . get_class($event->sender), PHP_EOL;
// });

// LogEvent::off(LogComponent::class, '*');
// LogEvent::off(LogComponent::class, LogComponent::EVENT_BEFORE_LOG);

// $log->encoding = 'text';

// 触发任何类的任何事件
// Event::on('*', '*', function ($event) {
//     echo('trigger event: ' . $event->name . PHP_EOL);
// });

// $logBehaviorConfig = [
//     'class' => LogBehavior::class,
//     'encoding' => 'json',
// ];
// $log->attachBehavior('stdLog', $logBehaviorConfig);
// $log->attachBehavior('stdLog', Yii::createObject($logBehaviorConfig));
// $log->cate('api')->level('error')->log('log');

// $log->log('hahah');
// LogEvent::trigger(LogComponent::class, LogComponent::EVENT_BEFORE_LOG);


// class Base{
//  public $name = '';
//  public function __construct(Depen $d)
//     {
//  }
// }

// class Depen
// {
// }

// $container = new Container;

// $base = $container->get('Base');

$dbConfig = [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=127.0.0.1;dbname=app',
    'username' => 'root',
    'password' => 'admin88',
    'charset' => 'utf8',
];
$db = Yii::createObject($dbConfig);
SlugModel::$db = $db;

$slug = new SlugModel;
$slug->attributes = [
    'id' => null,
    'name' => '你好',
    'route' => '#',
    'icon' => '',
    'pid' => 0,
    'extend' => '',
];
$slug->validate();
var_dump($slug->attributes);

/**
 * 处理 trace 参数列表
 * @param  mixed &$args 参数列表
 * @return mixed        处理结果
 */
function _trace(&$args)
{
    foreach ($args as &$arg) {
        if (is_array($arg)) {
            _trace($arg);
        } else {
            $arg = is_object($arg) ? get_class($arg) : $arg;
        }
    }
    return $args;
}

function echoTrace()
{
    $trace = debug_backtrace();
    array_shift($trace);
    $trace = _trace($trace);
    $out = '';
    foreach ($trace as $info) {
        $out .= getExecLine($info);
    }
    echo $out;
}

function getExecLine($info)
{
    $class = $info['class'] ?? '';
    $function = $info['function'] ?? '';
    $type = $info['type'] ?? '';
    $object = $info['object'] ?? '';
    $line = $info['line'] ?? '';
    $file = $info['file'] ?? '';
    $args = implode(', ', $info['args'] ?? []);

    $locate = (empty($file) and empty($line)) ? 'unknow' : "{$file}($line)\n";
    return "$locate\t{$class}{$type}{$function}($args)\n";
}
// var_dump($base);
